package com.itheima.controller;

import com.itheima.dto.MessageConstant;
import com.itheima.dto.PageResult;
import com.itheima.dto.QueryPageBean;
import com.itheima.dto.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
//import com.sun.org.apache.regexp.internal.RE;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author: leah_ana
 * @Date: 2022/3/31 13:04
 * 预约管理--检查组管理
 */

@RestController
@RequestMapping("/checkgroup")
public class CheckGroupController {

    @Autowired
    private CheckGroupService checkGroupService;

    /**
     * 条件查询检查组信息
     * @param queryPageBean
     * @return
     */
    @PostMapping("/findPage")
    public PageResult findPageBean(@RequestBody QueryPageBean queryPageBean) {

        return checkGroupService.findPageBean(queryPageBean);

    }

    /**
     * 新增检查组
     * @param checkGroup
     * @param ids
     * @return
     */
    @PostMapping
    public Result addCheckGroup(@RequestBody CheckGroup checkGroup, @RequestParam("checkitemIds") Integer[] ids) {
        boolean flag = checkGroupService.addCheckGroup(checkGroup, ids);
        return flag ? Result.success(MessageConstant.ADD_CHECKGROUP_SUCCESS)
                : Result.error(MessageConstant.ADD_CHECKGROUP_FAIL);

    }

    /**
     * 删除检查组
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteCheckGroup(@PathVariable Integer id) {
        boolean flag = checkGroupService.deleteCheckGroup(id);
        return flag ? Result.success(MessageConstant.DELETE_CHECKGROUP_SUCCESS)
                : Result.error("有套餐关联禁止删除");

    }

    /**
     * 根据id删除检查组
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        CheckGroup checkGroup = checkGroupService.findById(id);
        boolean flag = checkGroup != null;
        return flag ? Result.success(checkGroup, MessageConstant.QUERY_CHECKGROUP_SUCCESS)
                : Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);
    }

    /**
     * 根据id查询检查组关联的检查项id集合
     * @param id
     * @return
     */
    @GetMapping("/checkItem/{id}")
    public Result findCheckItemIdsByCheckGroupId(@PathVariable Integer id) {
        List<Integer> ids = checkGroupService.findCheckItemIdsByCheckGroupId(id);
        boolean flag = ids.size() != 0;
        return flag ? Result.success(ids, MessageConstant.QUERY_CHECKITEM_SUCCESS)
                : Result.error(MessageConstant.QUERY_CHECKITEM_FAIL);
    }

    /**
     * 编辑检查组
     * @param checkGroup
     * @param ids
     * @return
     */
    @PutMapping
    public Result editCheckGroup(@RequestBody CheckGroup checkGroup, @RequestParam("checkitemIds") Integer[] ids) {
        boolean flag = checkGroupService.editCheckGroup(checkGroup, ids);
        return flag ? Result.success(MessageConstant.EDIT_CHECKGROUP_SUCCESS)
                : Result.error(MessageConstant.EDIT_CHECKGROUP_FAIL);
    }

    /**
     * 查询所有检查组
     * @return
     */
    @GetMapping("/list")
    public Result findAll() {
        List<CheckGroup> groupList = checkGroupService.findAll();
        boolean flag = groupList.size() != 0;
        return flag ? Result.success(groupList, MessageConstant.QUERY_CHECKGROUP_SUCCESS)
                : Result.error(MessageConstant.QUERY_CHECKGROUP_FAIL);

    }


}
